package com.wzu.rjgc_ks.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wzu.rjgc_ks.dao.StudentMapper;
import com.wzu.rjgc_ks.pojo.Student;
import com.wzu.rjgc_ks.utils.ExcelUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/student")
public class StuAdminAction {

    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/anal")
    public String analStudent(ModelMap modelMap){

        return "admin/student/student_analysis";
    }

    @RequestMapping("/input")
    public String inputStudent(ModelMap modelMap){

        return "admin/student/student_input";
    }

    @RequestMapping("/show")
    public String showStudent(ModelMap modelMap){

        List<Student> list = studentMapper.selectAll();
        modelMap.addAttribute("stulist", list);

        return "admin/student/student_list";
    }

    @RequestMapping("/work")
    public String work(ModelMap modelMap){
        List<Student> list1= studentMapper.selectMajor();
        List<Student> list2= studentMapper.selectGrader();
        List<Student> list3= studentMapper.selectAll();
        List<Student> list4= studentMapper.selectWorkList();
        modelMap.addAttribute("stuMajor", list1);
        modelMap.addAttribute("stuGrader", list2);
        modelMap.addAttribute("stuList", list3);
        modelMap.addAttribute("workList", list4);

        return "admin/student/student_down";
    }

    @RequestMapping(value = "/imp2",method = RequestMethod.POST)
    public String imp(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request) throws Exception {
        if(!file.isEmpty()){
            String file_name = "success.xls";
            String filePath = "D:\\success.xls";
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType=file.getContentType();
            System.out.println(file_name);
            file.transferTo(new File(filePath));

            //解析excel
            List<Student> goodsList = ExcelUtil.importExcel(filePath,0,1,Student.class);
            //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
            System.out.println("导入数据一共【"+goodsList.size()+"】行");

            //TODO 保存数据库
            for (Student stu:goodsList) {
                System.out.println(stu);
                studentMapper.insert(stu);
            }
        }
        return "model";
    }

    @RequestMapping("/reset")
    public String reset(@RequestParam(value = "stuId") Integer stuId, ModelMap modelMap){
        //reset PWD
        Student student = new Student();
        student.setStuId(stuId);
        studentMapper.updatePWDByPrimaryKey(student);
        return "success";
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam(value = "stuId") Integer stuId, ModelMap modelMap){
        studentMapper.deleteByPrimaryKey(stuId);
        return "success";
    }


}
